/**
 *
 */
package com.goodsogood.union.auth.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.goodsogood.tool.result.ResponseResult;
import com.goodsogood.union.auth.conf.LogConfiger;
import com.goodsogood.union.auth.service.AccessToken;
import com.goodsogood.union.auth.service.AuthService;
import com.goodsogood.union.auth.service.PlateformDeviceType;
import com.goodsogood.union.auth.service.UserTokenService;
import com.goodsogood.union.auth.util.TokenUtil;

/**
 * @author 董国
 */
@Service("authService")
public class AuthServiceImpl implements AuthService {
	private static final Logger logger = LoggerFactory.getLogger(LogConfiger.APP);
	@Autowired
	private UserTokenService userTokenService;
	
	@Override
	public ResponseResult checkToken(Map<String, String> filter, HttpServletRequest request) {
		try {
			String token = filter.get("token");
			String plateformDeviceType = filter.get("plateformDeviceType");
			String plateformDeviceInfo = filter.get("plateformDeviceInfo");
			AccessToken accessToken = TokenUtil.decryptToken(token);
			String logOutNotifyMessage = userTokenService.findLogoutNotifyMessage(accessToken.getId());
			if (logOutNotifyMessage != null)
				return ResponseResult.failInstance(logOutNotifyMessage);

			return userTokenService.verify(token,plateformDeviceType,plateformDeviceInfo)  ? ResponseResult.successInstance()
					: ResponseResult.failInstance("令牌校验失败");
		} catch (Exception e) {
			logger.error("checkToken error !", e );
			return ResponseResult.failInstance("令牌校验失败");
		}
	}

	@Override
	public ResponseResult tokenGenerate(String plateformDeviceType, String userId,String plateformDeviceInfo) {
		try {
		    userTokenService.disbaleTokenOnOtherDevice(new PlateformDeviceType(plateformDeviceType), userId);
			AccessToken accessToken = new AccessToken(userId);
			accessToken.setPlateformDeviceType(plateformDeviceType);
			accessToken.setDeviceId(plateformDeviceInfo);
			userTokenService.addToken(userId, accessToken);
			Map<String, Object> map = new HashMap<String, Object>();
			Map<String, Object> clientTokenInfo = new HashMap<String, Object>();
			clientTokenInfo.put("userId", userId) ; 
			clientTokenInfo.put("id", accessToken.getId()) ; 
			map.put("token", TokenUtil.generate(JSON.toJSONString(clientTokenInfo)));
			map.put("expire_in", accessToken.getExpireIn());
			return ResponseResult.successInstance().setData(map);
		} catch (Exception e) {
			logger.error("tokenGenerate error !", e );
			return ResponseResult.failInstance("令牌生成失败");
		}

	}

	@Override
	public ResponseResult checkAuthAndToken(Map<String, String> filter) {
		String token = filter.get("token");
		//令牌校验
		if(!userTokenService.verify(token)){
			return ResponseResult.failInstance("令牌校验失败");
		}
		//权限校验
		if(!userTokenService.verifyAuth(token,filter.get("url"))){
			return ResponseResult.failInstance("用户没有此权限");
		}
		return ResponseResult.successInstance();
		
	}

}
